﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domin;
using Microsoft.EntityFrameworkCore;

namespace EF_Cord
{
    public class RDBC<T> : IRDomin<T> where T : class
    {
        private readonly MyDBCountext db;
        private readonly SDBCountext dbs;

        public RDBC(MyDBCountext db, SDBCountext dbs)
        {
            this.db = db;
            this.dbs = dbs;
        }

        public int Add(T model)
        {
            dbs.Set<T>().Add(model);
            return Ser();
        }

        public int Del(string id)
        {
            return dbs.Database.ExecuteSqlRaw($"delete from Peoples where PId in ({id})");
        }

        public int Ser()
        {
            return dbs.SaveChanges();
        }

        public IQueryable<T> Show()
        {
            return dbs.Set<T>().AsQueryable();
        }

        public int Upt(T model)
        {
            dbs.Set<T>().Update(model);
            return Ser();
        }

        public int HAdd(string ha, int id)
        {
            var ss =dbs.Peoples.Where(x => x.PId == id).ToList();
            var cc = ss.Count();
            int affectRows = dbs.Database.ExecuteSqlInterpolated(
                $"delete from Havs where PId ={id}");

            var dd = ha.Split(',');
            foreach (var item in dd)
            {
                affectRows += dbs.Database.ExecuteSqlInterpolated(
                    $"insert into Havs values({id},{item})");
            }
            return Ser();
        }
    }
}
